package com.sxfq.loansupermarket.backsystem.core.util.qr;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.sxfq.loansupermarket.backsystem.modular.weixin.service.IWxService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 内嵌图片的二维码生成器
 *
 * @author lichunxi
 */
public class ImgQrTool {

    @Autowired
    private IWxService wxService;

    public static void genQrcode(int width, int height, String format, String url, String savePath) {
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height);
            // 生成的二维码图片默认背景为白色,前景为黑色,但是在加入logo图像后会导致logo也变为黑白色,至于是什么原因还没有仔细去读它的源码
            // 所以这里对其第一个参数黑色将ZXing默认的前景色0xFF000000稍微改了一下0xFF000001,最终效果也是白色背景黑色前景的二维码,且logo颜色保持原有不变
            MatrixToImageConfig config = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF);
            // 这里要显式指定MatrixToImageConfig,否则还会按照默认处理将logo图像也变为黑白色(如果打算加logo的话,反之则不须传MatrixToImageConfig参数)
            // 开始画二维码
            BufferedImage barCodeImage = MatrixToImageWriter.toBufferedImage(bitMatrix, config);
            generateSaveFile(barCodeImage, savePath);
            System.out.println("qrcode write...");
        } catch (WriterException e) {
            e.printStackTrace();
        }

    }

    /**
     * 输出图片
     */
    public static void generateSaveFile(BufferedImage buffImg, String savePath) {
        int temp = savePath.lastIndexOf(".") + 1;
        try {
            File outFile = new File(savePath);
            if (!outFile.exists()) {
                outFile.createNewFile();
            }
            ImageIO.write(buffImg, savePath.substring(temp), outFile);
            System.out.println("ImageIO write...");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 为二维码图片增加logo头像
     *
     * @param image     二维码图片转化的BufferedImage对象
     * @param logoImage logo头像转化的BufferedImage对象
     * @see 其原理类似于图片加水印
     */
    public static BufferedImage overlapImage(BufferedImage image, BufferedImage logoImage) {
        int logoWidth = image.getWidth() / 5; // 设置logo图片宽度为二维码图片的五分之一
        int logoHeight = image.getHeight() / 5; // 设置logo图片高度为二维码图片的五分之一
        int logoX = (image.getWidth() - logoWidth) / 2; // 设置logo图片的位置,这里令其居中
        int logoY = (image.getHeight() - logoHeight) / 2; // 设置logo图片的位置,这里令其居中
        Graphics2D graphics = image.createGraphics();
        // 在图形和图像中实现混合和透明效果
        graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
        graphics.drawImage(logoImage, logoX, logoY, logoWidth, logoHeight, null);
        graphics.dispose();
        return image;
    }

    public static void genQrcodeTest(String id) {
        int width = 300;
        int height = 300;
        String format = "png";
//        String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQEr8TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyUzBNQ1lWRG04U1AxMDAwMGcwN1gAAgR0zPxdAwQAAAAA";
        String url = "http://test.yiyatest.cn:8787/integralMallWeixin/generateQrCode/" + id + "";
        String savePath = "D:\\8068\\apache-tomcat-8.0.30\\webapps\\ROOT\\newpy.png";
        genQrcode(width, height, format, url, savePath);
    }

    public static void logoQrcodeTest() {
        try {
            String imagePath = "D:\\8068\\apache-tomcat-8.0.30\\webapps\\ROOT\\newpy.png";
            File f = new File(imagePath);
            BufferedImage image = ImageIO.read(f);
            String logoPath = "D:\\8068\\apache-tomcat-8.0.30\\webapps\\ROOT\\loginpy.png";
            File logof = new File(logoPath);
            BufferedImage logoImage = ImageIO.read(logof);
            BufferedImage qrCodeImage = overlapImage(image, logoImage);
            String newPath = "D:\\8068\\apache-tomcat-8.0.30\\webapps\\ROOT\\logoNewpy.png";    //内嵌logo后新二维码地址
            generateSaveFile(qrCodeImage, newPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String id = "111";
//        //生成二维码
        genQrcodeTest(id);

        //二维码内嵌logo
        logoQrcodeTest();
    }
}
